package ua.com.cms.models;

import com.google.code.morphia.Morphia;
import com.google.code.morphia.dao.BasicDAO;
import com.google.code.morphia.mapping.Mapper;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;
import com.mongodb.Mongo;
import org.bson.types.ObjectId;

import ua.com.cms.entities.Slot;
import ua.com.cms.entities.Template;

import java.util.ArrayList;
import java.util.List;

public class TemplateDao extends BasicDAO<Template, ObjectId>{
    public TemplateDao(Mongo mongo, Morphia morphia, String dbName, String username, String password, boolean isAuthenticated) {
        super(mongo, morphia, dbName);
    }

    public Template findTemplatebyName(String name){
        Query<Template> query = getDatastore().createQuery(Template.class);
        return query.field("name").equal(name).get();
    }

    public Template findTemplatebyId(ObjectId id){
        Query<Template> query = getDatastore().createQuery(Template.class);
        return query.field("id").equal(id).get();
    }

    public Template get_or_create(String name, String path, String html, List<ObjectId> slots){
        Template template = findTemplatebyName(name);
        if(template == null){
            template = new Template();
            template.setHtml(html);
            template.setName(name);
            template.setPath(path);
            template.setSlots(slots);
            getDatastore().save(template);
        } else{
            UpdateOperations<Template> set = getDatastore().createUpdateOperations(Template.class);
            if(html!=null){
            	set.set("html", html);
            }
            if(name!=null){
            	set.set("name", name);
            }
            if(path!=null){
            	set.set("path", path);
            }
            if(slots!=null){
            	set.set("slots", slots);
            }
            Query<Template> updateQuery = getDatastore().createQuery(Template.class).field(Mapper.ID_KEY).equal(template.getId());
            getDatastore().update(updateQuery, set);
        }
        
        return template;
    }

    public void deleteTemplatebyName(String name){
        Query<Template> query = getDatastore().createQuery(Template.class);
        query = query.field("name").equal(name);
        getDatastore().delete(query);
    }

    public void deleteTemplatebyId(ObjectId id){
        Query<Template> query = getDatastore().createQuery(Template.class);
        query = query.field("id").equal(id);
        getDatastore().delete(query);
    }

    public void updateTemplatebyId(ObjectId currentId, String name, String path, String html, List<ObjectId> slots){
        deleteTemplatebyId(currentId);
        get_or_create(name, path, html, slots);
    }

    public void updateTemplatebyName(String currentName, String name, String path, String html, List<ObjectId> slots){
        deleteTemplatebyName(currentName);
        get_or_create(name, path, html, slots);
    }

    public void updateTemplate(Template template, List<ObjectId> slots){
        deleteTemplatebyName(template.getName());
        get_or_create(template.getName(), template.getPath(), template.getHtml(), slots);
    }

    /**
     * For debug.
     * @return
     */
	public List<String> getAllNames() {
        List<Template> allSlots = getAll();
        List<String> namesTemplate = new ArrayList<String>();
        for (Template slot : allSlots) {
        	namesTemplate.add(slot.getName()+" slots:"+slot.getSlots());
		}
        return namesTemplate;
	}

	public List<Template> getAll() {
		return getDatastore().find(Template.class).asList();
	}
}
